﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SY.WebApiTool.Interface;
using System.ComponentModel;
using SY.WebApiTool.Model;
using Newtonsoft.Json;

namespace SY.WebApiTool.Tool
{
    public class MaterialJsonFormatTool:IJsonFormatTool
    {
        string IJsonFormatTool.BatchSaveJson<T>(IEnumerable<T> parameters) 
        {           
           if(typeof(T) == typeof(Material))
            {
                MaterialSaveModel saveModel = new MaterialSaveModel { Model = (parameters as IEnumerable<Material>).ToArray() };
                return JsonConvert.SerializeObject(saveModel);
            }
            else
            {
                throw new Exception($"生成物料保存JSON时出错，传入BatchSaveJson方法的参数的泛型参数为{typeof(T).ToString()},应为Material");
            }
        }

        [Obsolete]
        string BatchSaveJson(IEnumerable<string> materialsString)
        {
            string jsonstring = "";
            StringBuilder builder = new StringBuilder();
            using (StreamReader reader =new StreamReader(typeof(MaterialJsonFormatTool).Module.Assembly.GetManifestResourceStream("SY.WebApiTool.JSON.MaterialSaveJson.json")                
                ))
            {
                jsonstring = reader.ReadToEnd();
            }
            foreach (var item in materialsString)
            {
                builder.Append(item);
                builder.Append(",");
            }
            builder.Remove(builder.Length - 1, 1);

            return jsonstring.Replace("\"BatchMaterials\"", builder.ToString());            
        }

        string IJsonFormatTool.BatchUpdateJson(IEnumerable<string> materialsString)
        {
            string jsonstring = "";
            StringBuilder builder = new StringBuilder();
            using (StreamReader reader = new StreamReader(typeof(MaterialJsonFormatTool).Module.Assembly.GetManifestResourceStream("SY.WebApiTool.JSON.MaterialUpdateJson.json")
                ))
            {
                jsonstring = reader.ReadToEnd();
            }
            foreach (var item in materialsString)
            {
                builder.Append(item);
                builder.Append(",");
            }
            builder.Remove(builder.Length - 1, 1);

            return jsonstring.Replace("\"BatchMaterials\"", builder.ToString());
        }


        string IJsonFormatTool.BatchSummitJson(IEnumerable<string> materialNumbers)
        {
            string jsonstring = "";
            StringBuilder builder = new StringBuilder();
            using (StreamReader reader = new StreamReader(typeof(MaterialJsonFormatTool).Module.Assembly.GetManifestResourceStream("SY.WebApiTool.JSON.MaterialSummitOrAuditJson.json")))
            {
                jsonstring = reader.ReadToEnd();
            }
            foreach (var item in materialNumbers)
            {
                builder.Append("\"");
                builder.Append(item);
                builder.Append("\",");
            }
            builder.Remove(builder.Length - 1, 1);

            return jsonstring.Replace("\"BatchMaterialNumber\"", builder.ToString());
        }

        string IJsonFormatTool.BatchAuditJson(IEnumerable<string> materialNumbers)
        {
            string jsonstring = "";
            StringBuilder builder = new StringBuilder();
            
            using (StreamReader reader = new StreamReader(typeof(MaterialJsonFormatTool).Module.Assembly.GetManifestResourceStream("SY.WebApiTool.JSON.MaterialSummitOrAuditJson.json")))
            {
                jsonstring = reader.ReadToEnd();
            }
            foreach (var item in materialNumbers)
            {
                builder.Append("\"");
                builder.Append(item);
                builder.Append("\",");
            }
            builder.Remove(builder.Length - 1, 1);

            return jsonstring.Replace("\"BatchMaterialNumber\"", builder.ToString());
        }

        
    }
}
